package com.sochinchou.res.items;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sochinchou.res.R;

public class MyAlertDialog extends Dialog {

	public MyAlertDialog(Context context, int theme) {
		super(context, theme);
	}

	public MyAlertDialog(Context context) {
		super(context);
	}

	/**
	 * Helper class for creating a custom dialog
	 */
	public static class Builder {

		private Context mContext;
		private Drawable mIcon;
		private String mTitle;
		private String mMessage;
		private String mPositiveButtonText;
		private String mNegativeButtonText;
		private String mNeturalButtonText;
		private View mContentView;

		private DialogInterface.OnClickListener mPositiveButtonClickListener, mNegativeButtonClickListener, mNeturalButtonClickListener;

		public Builder(Context context) {
			this.mContext = context;
		}

		public Builder setMessage(String message) {
			this.mMessage = message;
			return this;
		}

		public Builder setMessage(int message) {
			this.mMessage = mContext.getString(message);
			return this;
		}

		public Builder setTitle(int title) {
			this.mTitle = mContext.getString(title);
			return this;
		}

		public Builder setTitle(String title) {
			this.mTitle = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.mContentView = v;
			return this;
		}

		// public Builder setIcon(int resId){
		// mIcon = mContext.getResources().getDrawable(resId);
		// return this;
		// }
		//
		// public Builder setIcon(Drawable iconDrawable){
		// mIcon = iconDrawable;
		// return this;
		// }

		public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
			this.mPositiveButtonText = mContext.getString(positiveButtonText);
			this.mPositiveButtonClickListener = listener;
			return this;
		}

		public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
			this.mPositiveButtonText = positiveButtonText;
			this.mPositiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
			this.mNegativeButtonText = mContext.getString(negativeButtonText);
			this.mNegativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
			this.mNegativeButtonText = negativeButtonText;
			this.mNegativeButtonClickListener = listener;
			return this;
		}

		public Builder setNeturalButton(int neturalButtonText, DialogInterface.OnClickListener listener) {
			this.mNeturalButtonText = mContext.getString(neturalButtonText);
			this.mNeturalButtonClickListener = listener;
			return this;
		}

		public Builder setNeturalButton(String neturalButtonText, DialogInterface.OnClickListener listener) {
			this.mNeturalButtonText = neturalButtonText;
			this.mNeturalButtonClickListener = listener;
			return this;
		}

		public MyAlertDialog create() {

			final MyAlertDialog dialog = new MyAlertDialog(mContext, R.style.MyAlertDialog);
			View layout = LayoutInflater.from(mContext).inflate(R.layout.dialog_layout_myalert, null);

			// dialog.addContentView(layout, new
			// LayoutParams(LayoutParams.FILL_PARENT,
			// LayoutParams.WRAP_CONTENT));

			if (mPositiveButtonText != null) {
				((Button) layout.findViewById(R.id.positiveButton)).setText(mPositiveButtonText);
				if (mPositiveButtonClickListener != null) {
					((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							mPositiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
						}
					});
				}
			} else {
				layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
			}

			if (mNegativeButtonText != null) {
				// ((Button)
				// layout.findViewById(R.id.negativeButton)).setText(mNegativeButtonText);
				if (mNegativeButtonClickListener != null) {
					((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							mNegativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
						}
					});
				}
			} else {
				layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
			}

			if (mNeturalButtonText != null) {
				((Button) layout.findViewById(R.id.neturalButton)).setText(mNeturalButtonText);
				if (mNeturalButtonClickListener != null) {
					((Button) layout.findViewById(R.id.neturalButton)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							mNeturalButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
						}
					});
				}
			} else {
				layout.findViewById(R.id.neturalButton).setVisibility(View.GONE);
			}

			// if(mIcon == null && mTitle == null){
			// layout.findViewById(R.id.llTitle).setVisibility(View.GONE);
			// }else{
			// // set the dialog title
			// if(mTitle != null){
			// ((TextView) layout.findViewById(R.id.title)).setText(mTitle);
			// }else{
			// layout.findViewById(R.id.title).setVisibility(View.GONE);
			// }
			//
			// // set the dialog title
			// if(mIcon != null){
			// ((ImageView)
			// layout.findViewById(R.id.icon)).setImageDrawable(mIcon);
			// }else{
			// layout.findViewById(R.id.icon).setVisibility(View.GONE);
			// }
			//
			// }

			if (mTitle != null) {
				((TextView) layout.findViewById(R.id.title)).setText(mTitle);
			} else {
				layout.findViewById(R.id.title).setVisibility(View.GONE);
			}

			// set the dialog title
			// if(mIcon != null){
			// ((ImageView)
			// layout.findViewById(R.id.icon)).setImageDrawable(mIcon);
			// }else{
			// layout.findViewById(R.id.icon).setVisibility(View.GONE);
			// }

			// set the content message
			if (mMessage != null)
				((TextView) layout.findViewById(R.id.message)).setText(mMessage);
			else
				((TextView) layout.findViewById(R.id.message)).setVisibility(View.GONE);

			if (mContentView != null) {
				// if no message set, add the contentView to the dialog body
				// ((LinearLayout)
				// layout.findViewById(R.id.llContent)).removeAllViews();
				((LinearLayout) layout.findViewById(R.id.llContent)).addView(mContentView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			}
			dialog.setContentView(layout);
			WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
			params.width = (int) mContext.getResources().getDimension(R.dimen.width_alert_dialog);
			params.height = WindowManager.LayoutParams.WRAP_CONTENT;
			dialog.getWindow().setAttributes(params);
			return dialog;
		}

		public MyAlertDialog show() {
			MyAlertDialog dialog = create();
			dialog.show();
			return dialog;
		}

	}

}